package com.gotye.particlesystem.objects;

import android.graphics.Color;
import android.opengl.Matrix;
import android.util.Log;

import com.gotye.particlesystem.util.Geometry.Point;
import com.gotye.particlesystem.util.Geometry.Vector;

import java.util.Random;

/**
 * Created by Michael.Ma on 2016/6/14.
 */
public class ParticleFirework {
    private final static String TAG = "ParticleFirework";

    private final Point position;
    private final Vector direction;
    private final int color;

    private final float angleVariance = 180f;
    private final float speedVariance;

    private final Random random = new Random();

    private float[] rotationMatrix = new float[16];
    private float[] resultVector = new float[4];

    private float startTime;
    private boolean exploded;

    public ParticleFirework(Point position, Vector direction, int color, float speedVariance) {
        this.position = position;
        this.direction = direction;
        this.color = color;
        this.speedVariance = speedVariance;
    }

    public void fire(ParticleSystem particleSystem, float currentTime) {
        startTime = currentTime;
        exploded = false;
        particleSystem.addParticle(position, color, direction, currentTime);
    }

    public void update(ParticleSystem particleSystem, float currentTime) {
        if (currentTime - startTime > 2f && !exploded) {
            explode(particleSystem, currentTime, 500);
            Log.i(TAG, "firework exploded");
        }
    }

    public boolean isExploded() {
        return exploded;
    }

    private void explode(ParticleSystem particleSystem, float currentTime, int count) {
        float[] directionVector = new float[4];
        directionVector[0] = 0f;
        directionVector[1] = 0.25f;
        directionVector[2] = 0f;


        //vec3 currentPosition = a_Position + (a_DirectionVector * v_ElapsedTime);
        //float gravityFactor = v_ElapsedTime * v_ElapsedTime / 4.0;
        //currentPosition.y -= gravityFactor;
        float usedTime = currentTime - startTime;
        Vector v = new Vector(
                direction.x * usedTime,
                direction.y * usedTime - usedTime * usedTime / 4f,
                direction.z * usedTime
        );

        for (int i=0;i<count;i++) {
            Matrix.setRotateEulerM(rotationMatrix, 0,
                    (random.nextFloat() - 0.5f) * angleVariance,
                    (random.nextFloat() - 0.5f) * angleVariance,
                    (random.nextFloat() - 0.5f) * angleVariance);

            Matrix.multiplyMV(resultVector, 0,
                    rotationMatrix, 0,
                    directionVector, 0);

            float speedAdjustment = 1f + random.nextFloat() * speedVariance;

            Vector thisDirection = new Vector(
                    resultVector[0] * speedAdjustment,
                    resultVector[1] * speedAdjustment,
                    resultVector[2] * speedAdjustment
            );

            particleSystem.addParticle(position.translate(v), color, thisDirection,
                    currentTime);
        }

        exploded = true;
    }
}
